package com.sgb.zhaocai.view.demand.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ActivityProjectIssueListBinding;
import com.sgb.zhaocai.entity.ProjectIssueListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.adapter.ProjectIssueAdapter;
import com.sgb.zhaocai.viewmodel.ProjectIssueListModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.List;

/**
 * Time:2021/9/1
 * AUTHOR:F_Q
 * Description:项目发布列表
 */
public class ProjectIssueListActivity extends AppActivity {
    private RequestBean requestBean = new RequestBean();
    private ActivityProjectIssueListBinding mBinding;
    private ProjectIssueListModel mModel;
    private int PROJECTISSUE = 33;
    private ProjectIssueAdapter projectissueadapter;
    private boolean isLoadMore = false;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ProjectIssueListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_project_issue_list);
        mModel = new ViewModelProvider(this).get(ProjectIssueListModel.class);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
        mBinding.includeTitle.titleView.setText("项目列表");
        mBinding.includeTitle.rightView.setText("发布");
        //发布按钮点击
        mBinding.includeTitle.rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getApplicationContext(), ProjectIssueActivity.class).putExtra("type",2), PROJECTISSUE);
            }
        });
        //搜索
        mBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim())){
                    requestBean.setProjectName(mBinding.editSearch.getText().toString().trim());
                    initDataList();
                }else {
                    requestBean.setProjectName("");
                    initDataList();
                }
            }
        });
        mBinding.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length()>1){
                    mBinding.searchDelete.setVisibility(View.VISIBLE);
                }else {
                    mBinding.searchDelete.setVisibility(View.GONE);
                }
            }
        });
        mBinding.searchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.editSearch.setText("");
                mBinding.searchDelete.setVisibility(View.GONE);
                requestBean.setProjectName("");
                initDataList();
            }
        });
        if (projectissueadapter == null) {
            projectissueadapter = new ProjectIssueAdapter(getActivity(), null);
        }
        projectissueadapter.setOnItemStateOperate(new ProjectIssueAdapter.OnItemStateOperate() {
            @Override
            public void Publicity(ProjectIssueListEntity entity) {
                mModel.getIsPublicity(ProjectIssueListActivity.this, entity.getAnnounce(), entity.getId());
            }

            @Override
            public void CancelPublicity(ProjectIssueListEntity entity) {
                mModel.getIsPublicity(ProjectIssueListActivity.this, entity.getAnnounce(), entity.getId());
            }

            @Override
            public void Edit(ProjectIssueListEntity entity) {
                startActivityForResult(new Intent(getApplicationContext(), ProjectIssueActivity.class).putExtra("id",entity.getId()).putExtra("type",2), PROJECTISSUE);
            }

            @Override
            public void Details(ProjectIssueListEntity entity) {
                mModel.getWebH5(ProjectIssueListActivity.this, entity.getId());
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setAdapter(projectissueadapter);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                //上拉加载更多
                mModel.ProjectIssueList(ProjectIssueListActivity.this, requestBean);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        initDataList();
    }

    private void initDataList() {
        isLoadMore = false;
        requestBean.page = 1;
        mModel.ProjectIssueList(this, requestBean);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PROJECTISSUE) {
                //更新列表数据
                initDataList();
            }
        }
    }

    public void setProjectIssueList(List<ProjectIssueListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                projectissueadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                projectissueadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                projectissueadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    /**
     * 刷新数据列表
     */
    public void setIsPublicity() {
        initDataList();
    }
}
